package playchess;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import chuangkou.fuwuqi.FiveClient;
import chuangkou.login.UserChoice;
import history.Game;
import history.GameDao;
import history.Manual;
import history.ManualDao;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class MyJFrame extends JFrame implements MouseListener {
	private static final long serialVersionUID = 1L;
	public static void main(String[] args) {
		new MyJFrame().myJFrame("", "", "", "2021-01-12","记录人");
	}
	String ownName,opnName,comName,comDate;
	String winner,recorder;
	long blackTime,whiteTime,startTime;
	private long blacktime,whitetime;
	boolean flag = false;
	int qizi = 2;
	boolean over = true;
	boolean biaoshi = true;
	public long getBlacktime() {
		return blacktime;
	}
	public void setBlacktime(long blacktime) {
		this.blacktime += blacktime;
	}
	public long getWhitetime() {
		return whitetime;
	}
	public void setWhitetime(long whitetime) {
		this.whitetime += whitetime;
	}
	
	int qx = 20, qy = 40, qw = 525, qh = 525;//棋盘位置、宽高
	int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽高、位置
	int x = 0, y = 0;//保存棋子坐标
	int[][] saveGame = new int[16][16];//保存每个棋子
	int[][] step = new int[16][16];//下的步骤
	long[][] time = new long[16][16];
	int[][] jilu = new int[16][16];
//	ShunXu[] shunXu = new ShunXu[16*16];//记录旗子摆放顺序
	boolean flag2 = false;
	int num = 0;//记录总共下了几步
	int w = 0;
	int qc = 1;//记录白棋=2，黑棋=1
	int qn = 0;//判断棋子是否重复
	boolean canplay = true;//判断游戏是否开始和结束
	boolean huifang = false;
	String go = "黑子先行";//游戏信息
	FiveClient fc;
	
	long blackstartTime;
	long whitestartTime;
	
	
	//窗体
	public void myJFrame(String ownNames,String opnNames,String comNames,
			String comDates,String recorder) {
	ownName = ownNames;
	opnName = opnNames;
	comName = comNames;
	comDate = comDates;
	this.recorder = recorder;
	blackstartTime=System.currentTimeMillis();   //获取开始时间
	this.setTitle("五子棋"); //标题
	this.setSize(750, 600); //窗口大小
	this.setResizable(false); //窗口是否可以改变大小=否
	this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);
	int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度
	int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度
	this.setLocation((width - 800) / 2, (height - 600) / 2); 
	this.addMouseListener(this);
	this.setVisible(true); //窗口是否显示=是
	}
	
	public void myJFrame(String ownNames,String opnNames,String comNames,
		String comDates,String recorder,FiveClient fc,
		long[][] times,int[][] jilu,int num,String winner) {
		canplay = false;
		this.ownName = ownNames;
		this.opnName = opnNames;
		this.comName = comNames;
		this.comDate = comDates;
		this.time = times;
		this.jilu = jilu;
		this.recorder = recorder;
		this.fc = fc;
		this.num = num;
		this.winner = winner;
		this.huifang = true;
		this.biaoshi = false;
		this.setTitle("五子棋"); //标题
		this.setSize(750, 600); //窗口大小
		this.setResizable(false); //窗口是否可以改变大小=否
		this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度
		this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中
		this.addMouseListener(this);
		this.setVisible(true); //窗口是否显示=是
		this.flag2 = true;
		this.repaint();
		}
	//覆写paint方法,绘制界面
public void paint(Graphics g) {startTime =System.currentTimeMillis();
	BufferedImage bi = new BufferedImage(800, 600, BufferedImage.TYPE_INT_ARGB);
	Graphics g2 = bi.createGraphics();g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));
	g2.fillRect(qx - 20, qy - 20, qw + 40, qh + 40);//绘制棋盘背景矩形
	g2.setColor(Color.GRAY);g2.fillRect(bx, by - 60, bw, bh);
	g2.setFont(new Font("华文行楷", 10, 30));g2.setColor(Color.black);//设置画笔颜色
	g2.drawString("主界面", 600, 125);if(biaoshi) {g2.setColor(Color.GRAY);
		g2.fillRect(bx, by, bw, bh);g2.setFont(new Font("华文行楷", 10, 30));
		g2.setColor(Color.RED);g2.drawString("请点击棋盘", 572, 185);}
	if(biaoshi) {g2.setColor(Color.GRAY);g2.fillRect(bx, by + 60, bw, bh);//绘制实时存档按钮
		g2.setFont(new Font("华文行楷", 10, 30));g2.setColor(Color.WHITE);
		g2.drawString("实时存档", 585, 245);}
	if(biaoshi) {g2.setColor(Color.GRAY);
		g2.fillRect(bx, by + 120, bw, bh);g2.setFont(new Font("华文行楷", 10, 30));
		g2.setColor(Color.WHITE);g2.drawString("回放本局", 585, 305);//绘制字符
	}else {g2.setColor(Color.GRAY);g2.fillRect(bx, by + 120, bw, bh);g2.setFont(
	new Font("华文行楷", 10, 30));g2.setColor(Color.BLUE);g2.drawString("胜者:" + winner, 570, 305);}
	g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色
	g2.fillRect(565, 350, 200, 235);g2.setColor(Color.black);//设置画笔颜色
	g2.setFont(new Font("华文行楷", 30, 30));g2.drawString("游戏信息", 600, 377);
	g2.drawString("黑子:"+ownName,570,440);g2.drawString("白子:" + opnName, 570, 505);
	g2.drawString( comDate, 570, 570);g2.setColor(Color.RED);
	g2.drawString(String.valueOf(this.getBlacktime()/3600/1000) + ":"+ 
	String.valueOf(this.getBlacktime()/60/1000)+":"+
			String.valueOf(this.getBlacktime()/1000%60), 570, 470);
	g2.drawString(String.valueOf(this.getWhitetime()/3600/1000) + ":"+ 
	String.valueOf(this.getWhitetime()/60/1000) +":" + 
			String.valueOf(this.getWhitetime()/1000%60), 570, 535);
	g2.drawString(go, 570, 410);g2.setColor(Color.BLACK);//设置画笔颜色
	for (int x = 0; x <= qw; x += 35) {g2.drawLine(qx, x + qy, qw + qx, x + qy);
		g2.drawLine(x + qx, qy, x + qx, qh + qy);}
	for (int i = 3; i <= 11; i += 4) {for (int y = 3; y <= 11; y += 4) {
			g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);}}
	g2.setFont(new Font("黑体", 15, 15));for (int i = 0; i < 16; i++) {
	for (int j = 0; j < 16; j++) {if (saveGame[i][j] == 1){int sx = i * 35 + qx;
	int sy = j * 35 + qy;g2.setColor(Color.BLACK);g2.fillOval(sx - 13, sy - 13, 26, 26);}
	if (saveGame[i][j] == 2){int sx = i * 35 + qx;int sy = j * 35 + qy;
	g2.setColor(Color.WHITE);g2.fillOval(sx - 13, sy - 13, 26, 26);
	g2.setColor(Color.BLACK);g2.drawOval(sx - 13, sy - 13, 26, 26);}
	if(step[i][j] != 0) {if(step[i][j] % 2 == 1) {g2.setColor(Color.WHITE);
	}else {g2.setColor(Color.BLACK);}g2.drawString( String.valueOf(step[i][j]),
	i * 35 + qx -5,j * 35 + qy +5);}}}
	if(flag2) {if(w<num) {for(int x = 0;x<16;x++){for(int y = 0;y<16;y++) {
	if(jilu[x][y] == w +1) {step[x][y] = jilu[x][y];if(w % 2 == 0) {
	saveGame[x][y] = 1;blackTime = this.getBlacktime();
	this.setBlacktime(time[x][y]);}else {saveGame[x][y] = 2;
	whiteTime = this.getWhitetime();this.setWhitetime(time[x][y]);}}}}
	try {if(w % 2 == 0 ) {Thread.sleep((this.getBlacktime() - blackTime) );
	}else {Thread.sleep((this.getWhitetime() - whiteTime) );}
	} catch (InterruptedException e) {e.printStackTrace();}
	w++;this.repaint();}else {flag2 = false;}}g.drawImage(bi, 0, 0, this);}
	private boolean winLose() {boolean flag = false;int count = 1;
	int color = saveGame[x][y];int i = 1;if(x != 15) {while (color == saveGame[x + i][y]) {
		count++;i++;if(x+i >= 16) {break;}}}i = 1;
	if(x != 0) {while (color == saveGame[x - i][y]) {count++;i++;if(x-i < 0) {break;}}}
	if (count >= 5) {flag = true;}count = 1;i = 1;if(y != 15) {
		while (color == saveGame[x][y + i]) {count++;i++;if( y + i > 15) {break;}}}
	i = 1;if(y != 0) {while (color == saveGame[x][y - i]) {count++;i++;
	if( y -i < 0) {break;}}}if (count >= 5) {flag = true;}count = 1;i = 1;//迭代数
	if(x != 0 && y != 0) {while (color == saveGame[x - i][y - i]) {count++;i++;
	if(x-i < 0 || y -i < 0) {break;}}}i = 1;
	if(x != 15 && y != 15) {while (color == saveGame[x + i][y + i]) {count++;
	i++;if(x+i >= 16 || y + i > 15) {break;}}}
	if (count >= 5) {flag = true;}
	//判断斜向棋子是否相连（左下右上）
	count = 1;
	i = 1;//迭代数
	if(x != 15 && y !=0) {while (color == saveGame[x + i][y - i]) {
		count++;i++;if(x+i >= 16 || y -i < 0) {break;}}}
	i = 1;//迭代数
	if(x != 0 && y != 15) {while (color == saveGame[x - i][y + i]) {
		count++;i++;if(x-i < 0 || y + i > 15) {break;}}}
	if (count >= 5) {flag = true;}return flag;}
	//初始化游戏
	public void initialize() {
		this.flag = false;
		this.qizi = 1;
		this.over = true;
	//遍历并初始化数组
	for (int i = 0; i < 16; i++) {
		for (int j = 0; j < 16; j++) {
			saveGame[i][j] = 0;
			step[i][j] = 0;
		}
	}
		num = 0;
		//黑子先行
		qc = 1;
		go = "轮到黑子";
	}
	@Override//鼠标点击
	public void mouseClicked(MouseEvent e) {
	}
	@Override//鼠标按下
	public void mousePressed(MouseEvent e) {
		x = e.getX();y = e.getY();if(biaoshi) {
	if (e.getX() > bx && e.getX() < bx + bw && 
	e.getY() > by + 120 && e.getY() < by + 120 + bh) {
		if(canplay) {
	flag2 = true;w = 0;blacktime = 0;whitetime = 0;flag = false;
	over = false;for (int i = 0; i < 16; i++) {for (int j = 0; j < 16; j++) {
	saveGame[i][j] = 0;jilu[i][j] = step[i][j];step[i][j] = 0;}}
	this.repaint();}else {
	JOptionPane.showMessageDialog(this, "棋局还未记录完毕，记录完毕后可回放");}}}
	if (canplay) {if (x > qx && x < qx + qw && y > qy && y < qy + qh) {
	if ((x - qx) % 35 > 17) {x = (x - qx) / 35 + 1;} else {x = (x - qx) / 35;}
	if ((y - qy) % 35 > 17) {y = (y - qy) / 35 + 1;} else {y = (y - qy) / 35;}
	if (saveGame[x][y] == 0) {saveGame[x][y] = qc;qn = 0;} else {qn = 1;}
	if (qn == 0) {step[x][y] = num +1;this.flag = true;try {Thread.sleep(100);
	} catch (InterruptedException e1) {e1.printStackTrace();}
	long endTime=System.currentTimeMillis();if (qc == 1) {qc = 2;go = "轮到白子";
	time[x][y] = endTime - startTime;startTime=System.currentTimeMillis();   
	this.setBlacktime(time[x][y]);} else {qc = 1;go = "轮到黑子";
	time[x][y] = endTime - startTime;this.setWhitetime(time[x][y]);
	startTime=System.currentTimeMillis();}num++;}this.repaint();
	boolean wl = this.winLose();if (wl) {this.over = false;
	JOptionPane.showMessageDialog(this, "记录结束，" + 
	(saveGame[x][y] == 1 ? "黑方赢了" : "白方赢了"));canplay = true;
	winner = (saveGame[x][y] == 1 ? ownName : opnName);new SimpleDateFormat("yyyy-MM-dd");
	try {new GameDao().addGame(new Game(ownName,opnName, comDate, winner,recorder));
	} catch (IOException e1) {e1.printStackTrace();}
	Manual manual = new Manual(ownName, opnName, comDate, winner,recorder, num, step,time);
	new ManualDao().addManual(manual);}}}
	if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by - 60 && e.getY() < by - 60 + bh) {
			//判断游戏是否开始
			if (canplay || huifang) {
				//如果游戏结束，则返回主界面
				JOptionPane.showMessageDialog(this, "返回成功");
				this.dispose();
				new UserChoice(null,recorder,fc);
			} else {
				//如果游戏进行中，则判断是否操作错误
				JOptionPane.showMessageDialog(this, "棋局未结束，请先记录完本棋局");
			}
		}
		//实现实时存档按钮
		//判断是否点击按钮
	if(biaoshi) {
		if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 60 && e.getY() < by + 60 + bh) {
			huifang(ownName,opnName,comDate,recorder);
		}
	}}
	@Override//鼠标抬起
	public void mouseReleased(MouseEvent e) {
	}
	@Override//鼠标进入
	public void mouseEntered(MouseEvent e) {
	}
	@Override//鼠标离开
	public void mouseExited(MouseEvent e) {
	}
	
	public void huifang(String ownname,String opnname,String comdate,String recorders) {
		String ownName = ownname;
		String opnName = opnname;
		String comDate = comdate;
		String recorder = recorders;
		if (canplay) {
			//判断是谁认输
			if (qc == 1) {
				JOptionPane.showMessageDialog(this, "存档成功");
				winner = "pause";
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
				try {
			         sf.parse(comDate);
			     } catch (ParseException e1) {
			         e1.printStackTrace();
			     }
				try {
					new GameDao().addGame(new Game(ownName,opnName, 
							comDate, winner,recorder));
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				Manual manual = new Manual(ownName, opnName, 
						comDate, winner,recorder, num, step,time);
				new ManualDao().addManual(manual);
			} else if (qc == 2) {
				JOptionPane.showMessageDialog(this, "存档成功");
				winner = "pause";
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
				try {
			         sf.parse(comDate);
			     } catch (ParseException e1) {
			         e1.printStackTrace();
			     }
				try {
					new GameDao().addGame(new Game(ownName,opnName, 
							comDate, winner,recorder));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				Manual manual = new Manual(ownName, opnName, comDate, 
						winner,recorder, num, step,time);
				new ManualDao().addManual(manual);
			}
		} else {
			JOptionPane.showMessageDialog(this, "请先开始游戏");
		}
	}
	
	
	
}
